package com.study.services;

import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.study.model.jdao.ICollectionDAO;
import com.study.model.pojo.Colle;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.jdao.ICollectionDAO;
import com.study.model.jdao.IDictionaryDAO;
import com.study.model.jdao.ILinkmanDAO;
import com.study.model.mdao.ColleDAO;
import com.study.model.pojo.Colle;
import com.study.model.pojo.Dictionary;
import com.study.model.pojo.Form;
import com.study.model.pojo.FormsDetail;
import com.study.model.pojo.Linkman;

@Service
@Transactional(rollbackOn = Exception.class)
public class CollectionService {
	@Autowired
	ICollectionDAO dao;
	@Autowired
	ColleDAO mapper;
	@Autowired
	IDictionaryDAO iDao;
	@Autowired
	ILinkmanDAO linkmandao;
	@Autowired
	FormService formService;
	
	
	/**
	 * 添加一条采集信息
	 */
	public boolean addCollection(Colle colle) {
		boolean flag = false;
		try {
			dao.save(colle);
			flag = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 分页查询
	 */
	public List<JSONObject> selectByPager(int pageNum, int pageSize) {
		// 配置分页信息。这句话后面的第一个查询将调用分页。使用分页，对应的Service方法最好只写一个查询方法
		PageHelper.startPage(pageNum, pageSize);
		// 获取
		List<JSONObject> list = mapper.selectAll();
		System.out.println("大小" + list.size());
		return list;
	}

	/**
	 * 带条件的查询
	 * 
	 * @param pageNum
	 * @param pageNum
	 * @param input
	 * @param selects
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects) {
		String clazz = null;
		String status = null;
		
		PageHelper.startPage(pageNum, pageSize);
		
		if (selects.length() > 0 && selects.startsWith("171")) {
			status = selects;
		} else if (selects.length() > 0 && selects.startsWith("172")) {
			clazz = selects;
		}
		
		return mapper.selectByWhere(clazz, status, input);
	}
	/**
	 * 根据采集表的id查询单条记录
	 * ty
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		JSONObject colle = null;
		if (!"".equals(parameter)) {
			System.out.println(parameter);
			 colle = mapper.findByCid(parameter);
			 System.out.println(colle);
		}
		return colle;
	}
	
	/**
	 * 改变状态为 已处理
	 * @param parameter
	 */
	public void editorState(String parameter) {
		Colle colle = dao.findByColId(Integer.parseInt(parameter));
		colle.setDictionary2(iDao.findByDicId(1712));
	}

	/**
	 * 修改表达转化率
	 * @param id
	 * @param uid 
	 */
	public void editorForm(String id, int uid) {
		Colle colle = dao.findByColId(Integer.parseInt(id));
		FormsDetail formsDetail = colle.getFormsDetail();
		
		if (formsDetail != null ) {
			System.out.println("我正在转换成客户");
			int fordetailsId = formsDetail.getFordetailsId();
			Form form = formsDetail.getForm();
			int forId = form.getForId();
			
			formService.updateFormDetails(fordetailsId, uid);
			formService.updateForm(forId);
			
			//formsDetail.setForGuset(formsDetail.getForGuset()+1);
			//form.setForGuest(form.getForGuest()+1);
			
		}
	}

}
